package com.singzi.javaSenior.threads.basic;


public class AccountTest {
    public static void main(String[] args) {
        Account account = new Account(0);
        Customer customer1 = new Customer(account);
        Customer customer2 = new Customer(account);
        Thread  thread1 = new Thread(customer1);
        Thread  thread2 = new Thread(customer2);
        thread1.setName("甲");
        thread2.setName("乙");
        thread1.start();
        thread2.start();

    }
}
class Account{
    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }
    public synchronized void addBalance(double money) {
        this.balance += money;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "存钱成功，当前余额：" + this.balance);
    }
}

class Customer implements Runnable{
    private Account account;
    public  Customer(Account account){
        this.account = account;
    }

    @Override
    public void run() {
        for(int i = 0;i < 3;i++){
            account.addBalance(1000);
        }

    }
}